package adventure.placeable.trigger.conditions;

import player.Player;
import adventure.room.RoomManager;

/**
 *
 * @author Michael Hanns
 *
 */
public class RoomCondition extends TriggerCondition implements Cloneable {

    private int value;
    private int quantity;
    private String strval;

    public RoomCondition(String condition, int val, int quantity, String strval, boolean not) {
        super(condition, "ROOM", not);
        this.value = val;
        this.quantity = quantity;
        this.strval = strval;
    }

    public String getStringVal() {
        return strval;
    }

    public int getIntVal() {
        return value;
    }

    public int getQuantity() {
        return quantity;
    }

    @Override
    public String getDescription() {
        String notTxt;
        if(not()) {
            notTxt = "NOT ";
        } else {
            notTxt = "";
        }
        String description = notTxt + dependant + " " + condition + " ";

        if (hasValue()) {
            description = description + "Value: " + value + " ";
        }
        if (hasQuantity()) {
            description = description + "Quantity: " + quantity + " ";
        }

        return description;
    }

    private boolean hasValue() {
        if (condition.contains("PREVIOUSFAILED")) {
            return false;
        } else if (condition.equals("TITLEEQUALS")) {
            return false;
        } else if (condition.equals("HASCURRENCYOVER")) {
            return false;
        } else if (condition.equals("HASCURRENCYATLEAST")) {
            return false;
        } else if (condition.equals("HASCURRENCYUNDER")) {
            return false;
        } else if (condition.equals("HASCURRENCYATMOST")) {
            return false;
        }
        return true;
    }

    private boolean hasQuantity() {
        if (condition.contains("PREVIOUSFAILED")) {
            return false;
        } else if (condition.equals("TITLEEQUALS")) {
            return false;
        }
        return true;
    }

    @Override
    public boolean meetsCondition(RoomManager r, Player p, String i, boolean previousFailed) {
        boolean success = false;
        if (condition.equals("PREVIOUSFAILED")) {
            success = previousFailed;
        } else if (condition.equalsIgnoreCase("HASITEM")) {
            success = hasItem(r, p.x(), p.y(), p.z());
        } else if (condition.equalsIgnoreCase("HASMOBILE")) {
            success = hasMobile(r, p.x(), p.y(), p.z());
        } else if (condition.equalsIgnoreCase("HASTRIGGER")) {
            success = hasTrigger(r, p.x(), p.y(), p.z());
        } else if (condition.equalsIgnoreCase("HASCURRENCYOVER")) {
            success = hasOverCurrency(r, p.x(), p.y(), p.z());
        } else if (condition.equalsIgnoreCase("HASCURRENCYATLEAST")) {
            success = hasAtLeastCurrency(r, p.x(), p.y(), p.z());
        } else if (condition.equalsIgnoreCase("HASCURRENCYUNDER")) {
            success = hasUnderCurrency(r, p.x(), p.y(), p.z());
        } else if (condition.equalsIgnoreCase("HASCURRENCYATMOST")) {
            success = hasAtMostCurrency(r, p.x(), p.y(), p.z());
        } else if (condition.equalsIgnoreCase("TITLEEQUALS")) {
            success = titleEquals(r, p.x(), p.y(), p.z());
        } else {
            return false;
        }
        
        if(not()) {
            success = !success;
        }
        
        return success;
    }

    @Override
    public boolean isUserSpecified() {
        return false;
    }

    private boolean hasItem(RoomManager r, int x, int y, int z) {
        if (r.roomHasItem(x, y, z, value, quantity)) {
            return true;
        }
        return false;
    }

    private boolean hasMobile(RoomManager r, int x, int y, int z) {
        if (r.roomHasMobile(x, y, z, value, quantity)) {
            return true;
        }
        return false;
    }

    private boolean hasTrigger(RoomManager r, int x, int y, int z) {
        if (r.roomHasTrigger(x, y, z, value, quantity)) {
            return true;
        }
        return false;
    }

    private boolean hasOverCurrency(RoomManager r, int x, int y, int z) {
        return r.getCurrencyContents(x, y, z) > quantity;
    }

    private boolean hasAtLeastCurrency(RoomManager r, int x, int y, int z) {
        return r.getCurrencyContents(x, y, z) >= quantity;
    }

    private boolean hasUnderCurrency(RoomManager r, int x, int y, int z) {
        return r.getCurrencyContents(x, y, z) < quantity;
    }

    private boolean hasAtMostCurrency(RoomManager r, int x, int y, int z) {
        return r.getCurrencyContents(x, y, z) <= quantity;
    }

    private boolean titleEquals(RoomManager r, int x, int y, int z) {
        if (r.getRoomTitle(x, y, z).equalsIgnoreCase(strval)) {
            return true;
        }
        return false;
    }
}
